/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.utility;

import org.openaecfm.model.actor.Organization;
import org.openaecfm.model.utility.Application;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class ApplicationIO extends
		AbstractEntityInstanceInputImpl<Application> implements
		IEntityInstanceOutput<Application> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Application pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCAPPLICATION",
				appDataManager.getName(pojo));

		// Set ApplicationDeveloper
		EntityInstanceNameParameter developer = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getApplicationDeveloper()));
		ei.addParameter(developer);

		// Set Version
		StringParameter version = new StringParameter(pojo.getVersion(),
				StringParameter.UTF_8);
		ei.addParameter(version);

		// Set ApplicationFullName
		StringParameter name = new StringParameter(
				pojo.getApplicationFullName(), StringParameter.UTF_8);
		ei.addParameter(name);

		// Set ApplicationIdentifier
		StringParameter iden = new StringParameter(
				pojo.getApplicationIdentifier(), StringParameter.UTF_8);
		ei.addParameter(iden);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Application doCreateObject() {
		return new Application();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Application pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ApplicationDeveloper
		EntityInstanceNameParameter developer = (EntityInstanceNameParameter) sei
				.getParameter(0);
		pojo.setApplicationDeveloper((Organization) appDataManager
				.getObject(developer.toInstanceName()));
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			Application pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Version
		StringParameter version = (StringParameter) sei.getParameter(1);
		pojo.setVersion(version.toUTF8());

		// Set ApplicationFullName
		StringParameter name = (StringParameter) sei.getParameter(2);
		pojo.setApplicationFullName(name.toUTF8());

		// Set ApplicationIdentifier
		StringParameter iden = (StringParameter) sei.getParameter(3);
		pojo.setApplicationIdentifier(iden.toUTF8());

	}
}
